package com.dlnu.sujiwords.service.Impl;

import com.dlnu.sujiwords.dao.Memories_numberMapper;
import com.dlnu.sujiwords.dao.UserMapper;
import com.dlnu.sujiwords.domain.client.AjaxResult;
import com.dlnu.sujiwords.domain.pojo.Memories_number;
import com.dlnu.sujiwords.domain.pojo.User;
import com.dlnu.sujiwords.service.IUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements IUserService {

    @Resource
    private UserMapper userMapper;
    @Resource
    private Memories_numberMapper memoriesNumberMapper;


    /*修改用户*/
    @Override
    public AjaxResult updateUser(User update) {
        return userMapper.updateUser(update) > 0 ? AjaxResult.success("成功修改") : AjaxResult.success("修改失败请联系开发者");

    }

    /*创建用户*/
    @Override
    public AjaxResult createUser(User create) {
        if (userMapper.selectByPhoneNumber(create.getPhone_number()) != null) {
            return AjaxResult.success("该用户存在");
        }
        create.setUser_id(userMapper.selectAll().size());
        create.setRole((byte) 4);
        create.setOnline((byte) 0);
        System.out.println(create);
        if (userMapper.createUser(create) > 0) {
            memoriesNumberMapper.insert(new Memories_number(userMapper.selectByPhoneNumber(create.getPhone_number()).getUser_id()));
            return AjaxResult.success("成功创建");
        } else {
            return AjaxResult.success("创建失败,请联系开发者");
        }
    }

    /*查询在线用户*/
    @Override
    public AjaxResult selOnlineUser() {
        List<User> onlineUser = userMapper.selectOnline();
        if (onlineUser == null) {
            return AjaxResult.success("无在线用户");
        }
        return AjaxResult.success("查询成功", onlineUser);
    }

    /*查询用户*/
    @Override
    public AjaxResult selAllUser() {
        return AjaxResult.success("查询成功", userMapper.selectAll());
    }

    /*注册服务*/
    @Override
    public AjaxResult normalSign(User signUser) {
        User existUser = userMapper.selectByPhoneNumber(signUser.getPhone_number());
        if (existUser != null) {
            return AjaxResult.success("用户已存在");
        } else {
            userMapper.createNomerUser(signUser);
        }
        return AjaxResult.success("成功注册");
    }


    /*登录服务*/
    @Override
    public AjaxResult login(User loginUser) {
        String phoNum = loginUser.getPhone_number();
        String password = loginUser.getPassword();
        User queryUser = userMapper.selectByPhoneNumber(phoNum);
        if (queryUser == null) {
            return AjaxResult.success("登录失败,账号或密码错误");
        } else if (queryUser.passwordEqual(password)) {
            return AjaxResult.success("成功登录", queryUser);
        } else {
            return AjaxResult.success("登录失败,账号或密码错误");
        }
    }

    /*根据id删除用户*/
    @Override
    public AjaxResult delUserByid(Integer id) {

        return userMapper.deleteUserById(id) > 0 ? AjaxResult.success("删除成功") : AjaxResult.success("删除失败请联系开发者");
    }

    /*根据手机号查询用户*/
    @Override
    public AjaxResult selUserByPho(String pho) {
        User findUser = userMapper.selectByPhoneNumber(pho);
        return findUser == null ? AjaxResult.success("获取用户信息失败或已被删除") : AjaxResult.success("成功获取", findUser);
    }

    @Override
    public AjaxResult selUserLearnVocabulary(String pho) {
        return AjaxResult.success("成功查询", userMapper.getLearnVocabularyByPhoneNumber(pho));
    }

    @Override
    public AjaxResult updateLearnVocabulary(String phoneNumber, int vocabularyID) {
        return userMapper.updateUserLearnVocabulary(phoneNumber, vocabularyID) > 0 ? AjaxResult.success("成功更改") : AjaxResult.success("修改失败");
    }
}
